<?php

namespace App\Dao;

use App\Dao\Base\Dao;
use App\Model\GreetMaster;
use Hyperf\Context\ApplicationContext;

class GreetMasterDao
{
    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }

    // 随机获取一条打招呼数据
    public function getRandOne($uid, $master_id = 0)
    {
        $where[] = ['uid', '=', $uid];
        $where[] = ['status', '=', 1];

        if (!empty($master_id)) {
            $where[] = ['id', '=', $master_id];
        }

        $rs = GreetMaster::select(['id', 'uid', 'text', 'image_url', 'audio_url', 'audio_len', 'w', 'h'])->where($where)->inRandomOrder()->first();
        return modelToArray($rs);
    }


    /**
     * Note - 获取打招呼模板数据
     * DateTime 2024/8/14 15:18
     * User Admin
     * @param $uid
     * @param $master_ids
     * @return array
     */
    public function getGreets($uid, $master_ids)
    {
        if (!is_array($master_ids)) {
            $master_ids = [$master_ids];
        }
        // $where="`uid` = ? AND `status` = ?";
        // if(!empty($master_ids)){
        //     $where.=" and id in ({$master_ids})";
        // }
        // $sql = "SELECT id,uid,text,image_url,audio_url,audio_len,w,h FROM `$this->table` WHERE {$where}";
        // $model = $this->createCommand($sql);
        // $model->execute([$uid, 1]);
        // return $model->fetchAll(PDO::FETCH_ASSOC);
        $data = GreetMaster::query()->where('uid', $uid)->where('status', 1)->when(!empty($master_ids), function ($query) use ($master_ids) {
            $query->whereIn('id', $master_ids);
        })->get();
        return $data->toArray();
    }

}